import type { Request } from 'express';
import {
  responseSuccess,
  responseError,
  responseData,
} from '@/share/interface/standard';

import Base from '@/controller/public/MiniBaseController';
export class CapacityDiaryController extends Base {
  async getCapacityDiaryForUserToken(req: Request) {
    const user_id = parseInt(req.params.id) || req.user_id;
    // 获取最新的记录
    const latestRecord = await this.capacityDiaryRepository.findOne({
      where: { user_id },
      order: { created: 'DESC' },
    });

    // 检查最新记录的日期是否与当前日期相差一天或更多
    const today = new Date();
    today.setHours(0, 0, 0, 0); // 重置时间为今天的开始，忽略时分秒

    let newRecordNeeded = false;
    if (latestRecord) {
      const latestRecordDate = new Date(latestRecord.created);
      latestRecordDate.setHours(0, 0, 0, 0); // 重置时间为记录日期的开始

      const diffTime = today.getTime() - latestRecordDate.getTime();
      const diffDays = diffTime / (1000 * 60 * 60 * 24);

      if (diffDays >= 1) {
        newRecordNeeded = true;
      }
    } else {
      // 如果没有找到任何记录，也需要创建一个新记录
      newRecordNeeded = true;
    }

    // 如果需要，创建一个新的记录
    if (newRecordNeeded) {
      const newRecord = this.capacityDiaryRepository.create({
        user_id,
        capacity_record: [],
        user: { id: user_id },
      });
      await this.capacityDiaryRepository.save(newRecord);
      return responseSuccess(newRecord);
    }

    // 如果不需要新记录，返回最新的记录
    return responseSuccess(latestRecord);
  }
  async getCapacityDiaryForUser(req: Request) {
    const user_id = parseInt(req.params.id);
    const drug_reminder = await this.capacityDiaryRepository.find({
      where: { user_id },
    });
    return responseSuccess(drug_reminder);
  }
  async addCapacityTotalForUser(req: Request) {
    const { capacity_total, user_id = req.user_id } = req.body;
    if (isNaN(user_id)) {
      return responseError('无效的用户ID');
    }
    const user = await this.userRepository.findOne({
      where: { id: user_id },
    });
    if (!user)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });
    const diary = await this.capacityDiaryRepository.findOne({
      where: { user_id },
    });

    await this.capacityDiaryRepository.save({
      capacity_total,
      user_id,
      capacity_record: diary ? diary.capacity_record : [],
      id: diary?.id,
    });
    return responseSuccess();
  }
  async addCapacityRecordForUser(req: Request) {
    const { user_id = req.user_id, quantity } = req.body;
    if (isNaN(user_id)) {
      return responseError('无效的用户ID');
    }

    const user = await this.userRepository.findOne({
      where: { id: user_id },
    });

    if (!user)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });

    let diary = await this.capacityDiaryRepository.findOne({
      where: { user_id },
      order: { created: 'DESC' },
    });

    if (!diary) {
      // return responseError('请先添加喝水总量');
      diary = this.capacityDiaryRepository.create({
        user_id,
        user: { id: user_id },
        capacity_record: [],
      });
      await this.capacityDiaryRepository.save(diary);
    }
    diary.capacity_record = diary.capacity_record || [];
    diary.capacity_record.unshift({ quantity, time: new Date() });

    await this.capacityDiaryRepository.save({
      capacity_record: diary.capacity_record,
      // progress: diary.progress,
      user_id,
      id: diary?.id,
    });

    return responseSuccess();
  }
  // 清空喝水记录
  async clearCapacityRecordForUserToken(req: Request) {
    const user_id = req.user_id;

    const user = await this.userRepository.findOne({
      where: { id: user_id },
    });
    if (!user)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });
    const diary = await this.capacityDiaryRepository.findOne({
      where: { user_id },
    });
    if (!diary)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });

    await this.capacityDiaryRepository.save({
      capacity_record: [],
      progress: 0,
      id: diary.id,
    });
    return responseSuccess();
  }
  async clearCapacityRecordForUser(req: Request) {
    const user_id = parseInt(req.params.id);

    const user = await this.userRepository.findOne({
      where: { id: user_id },
    });
    if (!user)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });
    const diary = await this.capacityDiaryRepository.findOne({
      where: { user_id },
    });
    if (!diary)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });

    await this.capacityDiaryRepository.save({
      capacity_record: [],
      progress: 0,
      id: diary.id,
    });
    return responseSuccess();
  }

  async setOutPutRecord(req: Request) {
    const user_id = req.user_id;
    const {
      total_output,
      defecation_volume,
      urination_volume,
      total_input,
    } = req.body;
    const diary = await this.capacityDiaryRepository.findOne({
      where: { user_id },
      order: { created: 'DESC' },
    });
    if (!diary)
      return responseData({
        code: 201,
        msg: '用户不存在',
        data: null,
      });
    await this.capacityDiaryRepository.save({
      total_input,
      total_output,
      defecation_volume,
      urination_volume,
      id: diary.id,
      user: { id: user_id },
    });
    return responseSuccess();
  }
}
